package xyz.xpman.prisonerfitness.adapter;

import android.graphics.Bitmap;
import android.os.Build;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import xyz.xpman.prisonerfitness.entity.Mappings;
import xyz.xpman.prisonerfitness.entity.TrainingRecord;

@SuppressWarnings("unused")
@RequiresApi(api = Build.VERSION_CODES.O)
public class TrainingRecordCache implements IViewCache<TrainingRecord> {
    private TextView dateView;
    private TextView titleView;
    private ImageView movesFlagImgView;
    private TextView totalTimeView;
    private TextView totalTimesView;
    private TextView totalGroupsView;


    public static TrainingRecordCache by(
            @NotNull TextView dateView,
            @NotNull TextView titleView,
            @NotNull ImageView movesFlagImgView,
            @NotNull TextView totalTimeView,
            @NotNull TextView totalTimesView,
            @NotNull TextView totalGroupsView) {

        TrainingRecordCache cache = new TrainingRecordCache();

        cache.dateView = dateView;
        cache.titleView = titleView;
        cache.movesFlagImgView = movesFlagImgView;
        cache.totalTimeView = totalTimeView;
        cache.totalTimesView = totalTimesView;
        cache.totalGroupsView = totalGroupsView;

        return cache;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    private void setTitle(String title) {
        titleView.setText(title);
    }

    private void setDate(String date) {
        dateView.setText(date);
    }

    private void setMovesFlagImg(int imgId) {
        movesFlagImgView.setImageResource(imgId);
    }

    private void setMovesFlagImg(Bitmap img) {
        movesFlagImgView.setImageBitmap(img);
    }



    private void setTotalTime(@NotNull String totalTime) {
        totalTimeView.setText(totalTime);
    }

    private void setTotalTimes(@NotNull String totalTimes) {
        totalTimesView.setText(totalTimes);
    }

    private void setTotalGroups(@NotNull String totalGroups) {
        totalGroupsView.setText(totalGroups);
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    @Override
    public void process(int position, @NotNull TrainingRecord item) {
        // Log.i("TrainingRecordCache", position + ": " + item);

        // 2021-06-21 19:12:21:121
        String date = item.getDate()
                .substring(0, "2021-06-21".length())
                .replace("-", ".");
        setDate(date);


        Bitmap img = Mappings.getMovesGuidImg(item.getMovesId());

        if (null != img) {
            setMovesFlagImg(img);
        }


        setTitle(Mappings.getMovesTitle(item.getMovesId())
               + Mappings.getChapterTitle(item.getChapterId()));

        setTotalTime(String.valueOf(item.getUsedTime()));
        setTotalTimes(String.valueOf(item.getStandardTimes()));
        setTotalGroups(String.valueOf(item.getStandardGroups()));

    }

}
